import {ItemInterface} from "./itemInterface";
import {CreateDomWithList} from "./CreateDomWithList";
import {ConfigureElementForVNode} from "./ConfigureElementForVNode";
import {init} from "snabbdom/src/package/init";
import {styleModule} from "snabbdom/src/package/modules/style";
import {eventListenersModule} from "snabbdom/src/package/modules/eventlisteners";


let patch = init([
    styleModule, eventListenersModule
]);

let AddSingleItem = (originalData:Array<ItemInterface>) => {
    let oldDOM = CreateDomWithList(originalData);
    ConfigureElementForVNode(oldDOM);

    let ranks = originalData.map(e => e.rank).sort((a, b) => b - a);
    let rank = Number(ranks[0]) + 1; //获取最大值
    let getRandomValue = () => Math.floor(Math.random() * originalData.length);
    let title = originalData[getRandomValue()].title;
    let desc = originalData[getRandomValue()].desc;

    originalData.push({rank, title, desc});

    let newDOM = CreateDomWithList(originalData);
    ConfigureElementForVNode(newDOM);
    patch(oldDOM, newDOM);

};

export {AddSingleItem};